<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<script>
		// let arr = []
		// arr[5] = 1
		// arr[3] = 1
		// arr[6] = 1
		// arr[7] = 1
		// arr[1] = 1
		// arr[2] = 1
		// arr.forEach((item, index) => {
		// 	console.log(item, index);
		// })

		function findMax(arr) {
			let base = arr[0]
			for (let i = 0; i < arr.length; i++) {
				if (arr[i] > base) {
					base = arr[i];
				}
			}
			return base
		}

		function countSort(arr) {
			// const maxValue = Math.max(...arr)
			const maxValue = findMax(arr)
			const counts = new Array(maxValue + 1)
			arr.forEach(item => {
				if(!counts[item]) counts[item] = 0
				counts[item]++
			})
			const newArr = []
			counts.forEach((item, index) => {
				while (item > 0) {
					newArr.push(index)
					item--
				}
			})
			return newArr
		}
		console.log(countSort([5,3,5,6,7,1,9,2]));

	</script>
</body>
</html>